package com.oocl.o2o.merchant.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Controller("wsHandler")
@RequestMapping("/ws/audit")
public class WsController extends TextWebSocketHandler {
	private Map<String, WebSocketSession> sessions = new HashMap<>();
	@Override
	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
		Map<String, Object> map = session.getAttributes();
		String username = (String) map.get("username");
		System.out.println(username);
		System.out.println(session.getId());
		sessions.put(username, session);
	}
	public void sendMessage(String username,String content) {
		try {
			System.out.println(content);
			if(sessions.get(username)==null)
				return;
			sessions.get(username).sendMessage(new TextMessage(content));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	

}
